package client

import (
	"imooc.com/crawler/engine"
	"log"
	"imooc.com/crawler_distrilbuted/rpcsupport"
	"imooc.com/crawler_distrilbuted/config"
)

func ItemSaver(host string) (chan engine.Item, error){

	client, err := rpcsupport.NewClient(host)
	if err != nil {
		return nil, err
	}
	out := make(chan engine.Item)
	go func() {
		itemCount := 0
		for  {
			item := <- out
			log.Printf("Item Saver: go item #%d: %v", itemCount, item)
			itemCount++
			result := ""
			err = client.Call(config.ItemSaveRpc, item, &result)
			if err != nil {
				log.Printf("result: %s; error: %s",
					item, err)
			}
		}
	}()
	return out, nil
}